namespace 第四次课堂作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;

            System.Timers.Timer t1 = new System.Timers.Timer(1000);

            t1.Elapsed += new System.Timers.ElapsedEventHandler(DiDa);

            t1.AutoReset = true;

            t1.Enabled = true;
        }
        int didaNum = 0;//“滴答”的个数
        public void DiDa(object source, System.Timers.ElapsedEventArgs e)  //滴答事件
        {
            CH.Text = DateTime.Now.Hour.ToString();
            CM.Text = DateTime.Now.Minute.ToString();
            CS.Text = DateTime.Now.Second.ToString();
            if (!isLing)
            {
                closeBtn.Visible = false;
            }
            if (didaNum > 9)
            {
                didaNum = 0;
                didaLabel.Text = "";
            }
            didaLabel.Text += "滴答   ";
            didaNum++;
        }
        int lingNum = 0; //“叮铃铃”的个数
        bool isLing = false;
        public void Ling(object source, System.Timers.ElapsedEventArgs e)  //打铃事件
        {
            string aH = AH.Text;
            string aM = AM.Text;
            string aS = AS.Text;
            try
            {
                if(aH == "" || aM == "" || aS == "")
                {
                    isBegin = false;
                    throw new ArgumentNullException();
                }
                if (int.Parse(aH) < 0 || int.Parse(aH) > 23)
                {
                    isBegin = false;
                    throw new MyException("时：请输入0至23的整数\n");
                }
                if(int.Parse(aM) < 0 || int.Parse(aM) > 59)
                {
                    isBegin = false;
                    throw new MyException("分：请输入0至59的整数\n");
                }
                if (int.Parse(aS) < 0 || int.Parse(aS) > 59)
                {
                    isBegin = false;
                    throw new MyException("秒：请输入0至59的整数\n");
                }
                tipLabel.Text = "闹铃启动中"; 
                //到时刻响铃
                if ((int.Parse(aH) == int.Parse(CH.Text)) && (int.Parse(aM) == int.Parse(CM.Text)) && (int.Parse(aS) == int.Parse(CS.Text))) 
                { 
                    isLing = true;
                    closeBtn.Visible = true;
                    while (isLing)
                    {
                        Thread.Sleep(1000);
                        if (lingNum > 6)
                        {
                            lingNum = 0;
                            lingLabel.Text = "";
                        }
                        lingLabel.Text += "叮铃铃   ";
                        lingNum++;
                        if (!isLing) lingLabel.Text = "";
                    }
                }
            }
            catch (ArgumentNullException ex) //处理输入为空异常
            {
                tipLabel.Text = "时、分、秒输入不能为空！！！";
                t.Enabled = false;
                t.Elapsed -= new System.Timers.ElapsedEventHandler(Ling);
                isBegin = false;
            }
            catch (FormatException ex)  //处理输入类型错误异常
            {
                tipLabel.Text = "时、分、秒请输入整数";
                t.Enabled = false;
                t.Elapsed -= new System.Timers.ElapsedEventHandler(Ling);
                isBegin = false;
            }
            catch (MyException myException)  //处理输入整数范围错误异常
            {
               tipLabel.Text += myException.Message + "  ";
                t.Enabled = false;
                t.Elapsed -= new System.Timers.ElapsedEventHandler(Ling);
            }
        }
        System.Timers.Timer t = new System.Timers.Timer(1000);
        bool isBegin = false;   //是否启动闹铃
        private void button1_Click(object sender, EventArgs e)  //启动闹铃按钮事件
        {
            if (!isBegin)
            {
                t.Elapsed += new System.Timers.ElapsedEventHandler(Ling);

                t.AutoReset = true;

                t.Enabled = true;

                isBegin = true;

                tipLabel.Text = "";
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)  //取消闹铃按钮事件
        {
            t.Enabled = false;
            t.Elapsed -= new System.Timers.ElapsedEventHandler(Ling);
            isBegin = false;
            tipLabel.Text = "";
        }
        private void closeBtn_Click(object sender, EventArgs e)  //关闭闹铃按钮事件
        {
            isLing = false;
            tipLabel.Text = "";
            t.Elapsed -= new System.Timers.ElapsedEventHandler(Ling);
            isBegin = false;
            closeBtn.Visible = false;
        }
    }
    class MyException : Exception //自定义异常（限制输入数字在范围内）
    {
        public MyException(string message):base(message)
        {
        }
    }
}